<?php

/**
 * Created by PhpStorm.
 * Script Name: EventAuthorizedHandler.php
 * Create: 2020/4/15 10:54
 * Description: 第三方公众号授权成功处理器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace plugin\wechat\app\platform\handler\platform;

use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use plugin\wechat\app\service\Mp as MpService;
use plugin\wechat\app\service\Mini as MiniService;
use plugin\wechat\app\service\OpenPlatform as OpenPlatformService;

class EventAuthorizedHandler implements EventHandlerInterface
{

    /**
     * 处理器
     * @param mixed $payload
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @author fudaoji<fdj@kuryun.cn>
     */
    public function handle($payload = null) {
        $authorizer_info = OpenPlatformService::getOpenPlatform()->getAuthorizer($payload['AuthorizerAppid']);
        if(isset($authorizer_info['authorizer_info']['MiniProgramInfo'])) {
            //小程序类型授权
            MiniService::updateAuthInfo($authorizer_info);
        }else{
            //公众号类型授权
            MpService::updateAuthInfo($authorizer_info);
        }
    }
}